﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class TaoBaoApi_TB_DiQu : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindArea();
    }
    /// <summary>
    /// 绑定地区列表
    /// </summary>
    void BindArea()
    {
        this.treeView1.Nodes.Clear();
        List<Model.TaoBao.TB_DIQU> areaArray = new BLL.TaoBao.TB_DIQU().GetAllList();       
        foreach (Model.TaoBao.TB_DIQU area in areaArray)
        {
            if (area.PID == -1)
                treeView1.Nodes.Add(new TreeNode(area.DQMC, area.DQDM));
            else
            {
                foreach (TreeNode n in treeView1.Nodes)
                {
                    if (n.Depth == 0)
                    {
                        int mid = new TBCommon().GetTBAreaMID(n.Value,areaArray);
                        if (area.PID == mid)
                        {
                            n.ChildNodes.Add(new TreeNode(area.DQMC, area.DQDM));
                            break;
                        }
                    }
                }
            }
        }
    }
    //清空地区数据库
    void ClearArea()
    {
        List<Model.TaoBao.TB_DIQU> areaList = new BLL.TaoBao.TB_DIQU().GetAllList();
        List<int> midList = new List<int>();
        foreach (Model.TaoBao.TB_DIQU area in areaList)
            midList.Add(area.MID);
        new BLL.TaoBao.TB_DIQU().Delete(midList);
    }
    protected void btnDownArea_Click(object sender, EventArgs e)
    {
        ClearArea();
        string[,] area = new TaoBaoAPI().GetTBArea();
        if (area == null || area.Length == 0)
            return;        
        for (int i = 0; i < area.Length/3; i++)
        {
            Model.TaoBao.TB_DIQU areaModel = new Model.TaoBao.TB_DIQU();
            areaModel.DQDM = area[i, 0];
            if (area[i, 1].IndexOf(",") < 0)
            {
                areaModel.DQMC = area[i, 1];
                areaModel.LAYER = 0;
                areaModel.PID = -1;
                areaModel.ARRANGE = ",";                
            }
            else
            {
                areaModel.DQMC = area[i, 1].Substring(0, area[i, 1].IndexOf(","));
                areaModel.LAYER = 1;
                int mid = new TBCommon().GetTBAreaMID(area[i, 2]);
                areaModel.PARENTID = mid;
                areaModel.LAYER = 1;
                areaModel.PID = mid;
                areaModel.ARRANGE = "," + mid.ToString() + ",";
            }
            if (new BLL.TaoBao.TB_DIQU().Add(areaModel) < 0)
                return;
        }
        Response.Redirect("tb_diqu.aspx");
    }
    protected void treeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (treeView1.SelectedNode != null)
        {
            txtCode.Text = treeView1.SelectedNode.Value;
            txtName.Text = treeView1.SelectedNode.Text;
        }
    }
}
